package cn.luohailiang.natruallanguage;

import android.icu.text.SimpleDateFormat;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.sinovoice.hcicloudsdk.api.HciCloudSys;
import com.sinovoice.hcicloudsdk.common.AuthExpireTime;
import com.sinovoice.hcicloudsdk.common.HciErrorCode;
import com.sinovoice.hcicloudsdk.common.InitParam;

import java.io.File;
import java.util.Date;
import java.util.Locale;


public class PermissionActivity extends AppCompatActivity {

    private static String TAG="----------------------------->";

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_permission);
        checkAuthAndUpdateAuth();
    }

    private void primissionAsk()
    {
        //授权文件路径 默认在文件缓存
        String authDirPath = this.getFilesDir().getAbsolutePath();
        // 前置条件：无
        InitParam initparam = new InitParam();
        // 授权文件所在路径，此项必填
        initparam.addParam(InitParam.AuthParam.PARAM_KEY_AUTH_PATH, authDirPath);
        // 灵云云服务的接口地址，此项必填
        initparam.addParam(InitParam.AuthParam.PARAM_KEY_CLOUD_URL,  "test.api.hcicloud.com:8888");
        // 开发者Key，此项必填，开发者社区申请所得
        initparam.addParam(InitParam.AuthParam.PARAM_KEY_DEVELOPER_KEY, "7dafh5eb2dcdb5a1ab78af0000000000");
        // 应用Key，此项必填，开发者社区申请所得
        initparam.addParam(InitParam.AuthParam.PARAM_KEY_APP_KEY, "60665666");

        // 配置日志参数
        String sdcardState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(sdcardState)) {
            String sdPath = Environment.getExternalStorageDirectory().getAbsolutePath();
            String packageName = this.getPackageName();
            String logPath = sdPath + File.separator + "sinovoice" + File.separator
                    + packageName + File.separator + "log" + File.separator;

            // 日志文件地址
            File fileDir = new File(logPath);
            boolean exit= fileDir.exists();
            if (!exit) {
                fileDir.mkdirs();
            }

            // 日志的路径，可选，如果不传或者为空则不生成日志
            initparam.addParam(InitParam.LogParam.PARAM_KEY_LOG_LEVEL, "5");
            initparam.addParam(InitParam.LogParam.PARAM_KEY_LOG_FILE_PATH, logPath);
        }
        // 初始化 errocode可从开发手册查询到
        int errCode = HciCloudSys.hciInit(initparam.getStringConfig(), this);
    }

    // 获取授权
    private int checkAuthAndUpdateAuth() {

        // 获取系统授权到期时间
        int initResult;
        AuthExpireTime objExpireTime = new AuthExpireTime();
        initResult = HciCloudSys.hciGetAuthExpireTime(objExpireTime);
        if (initResult == HciErrorCode.HCI_ERR_NONE) {
            // 显示授权日期,如用户不需要关注该值,此处代码可忽略
            Date date = new Date(objExpireTime.getExpireTime() * 1000);
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
            Log.i(TAG, "expire time: " + sdf.format(date));

            if (objExpireTime.getExpireTime() * 1000 > System.currentTimeMillis()) {
                // 已经成功获取了授权,并且距离授权到期有充足的时间(>7天)
                Log.i(TAG, "checkAuth success");
                return initResult;
            }
        }

        // 获取过期时间失败或者已经过期
        initResult = HciCloudSys.hciCheckAuth();
        if (initResult == HciErrorCode.HCI_ERR_NONE) {
            Log.i(TAG, "checkAuth success");
            return initResult;
        } else {
            Log.e(TAG, "checkAuth failed: " + initResult);
            return initResult;
        }
    }

}
